package com.docios.cookbook2.ui.otherPager;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.docios.cookbook2.databinding.ActivityCategoriesBinding;
import com.docios.cookbook2.entity.Category;
import com.docios.cookbook2.ui.adapter.ItemCategoryAdapter;
import com.docios.cookbook2.utils.JsonUtil;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

public class CategoriesActivity extends AppCompatActivity {
    ActivityCategoriesBinding catBinding;
    List<Category> categories;
    //用于 跳转位置 和 加载布局
    LinearLayoutManager llManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        catBinding = ActivityCategoriesBinding.inflate(getLayoutInflater());
        setContentView(catBinding.getRoot());

        localJsonData();
        initView();
        catBinding.catToolBar.setNavigationOnClickListener(f -> finish());
        btnSearchOnClick();
        tabOnClick();

    }


    private void localJsonData() {
        try {
            categories = new JsonUtil().localClasses(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        //标签布局
        this.runOnUiThread(() -> {
            for (Category category : categories) {
                catBinding.catTab.addTab(catBinding.catTab.newTab().setText(category.getName()));
            }
        });

        //分类菜单布局
        this.runOnUiThread(() -> {
            ItemCategoryAdapter categoryAdapter = new ItemCategoryAdapter(categories, this);
            catBinding.catRecycle.setAdapter(categoryAdapter);
            llManager = new LinearLayoutManager(this);
            llManager.setOrientation(LinearLayoutManager.VERTICAL);
            catBinding.catRecycle.setLayoutManager(llManager);
        });

    }


    private void btnSearchOnClick() {
        catBinding.catSearch.setOnClickListener(view -> {
            Intent intent = new Intent(this, CommonChildActivity.class);
            intent.putExtra("flag", "home_search");
            //动画
            ActivityOptions animation = ActivityOptions
                    .makeSceneTransitionAnimation(this, view, "search_bar");

            startActivity(intent, animation.toBundle());
        });
    }

    private void tabOnClick() {
        catBinding.catTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                moveToPosition(llManager,tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * RecyclerView 移动到当前位置，
     *
     * @param manager  设置RecyclerView对应的manager
     * @param n  要跳转的位置
     */
    public static void moveToPosition(LinearLayoutManager manager, int n) {
        manager.scrollToPositionWithOffset(n, 0);
        manager.setStackFromEnd(true);
    }

}